package com.young.shop.service.impl;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.young.shop.controller.SendEmailController;
import com.young.shop.dao.UserDao;
import com.young.shop.dao.impl.UserDaoImpl;
import com.young.shop.pojo.entity.User;
import com.young.shop.service.UserService;

import javax.servlet.http.HttpServletRequest;

public class UserServiceImple implements UserService {
    private UserDao userDao=new UserDaoImpl();

    @Override
    public User save(User user) {
        int result = userDao.insert(user);
        if (result !=1){
            throw new RuntimeException("用户信息保存失败");
        }
        User dbUser = userDao.selectByEmail(user.getEmail());
        return dbUser;
    }

    @Override
    public boolean existsByUserEmail(User user) {
        String userName = user.getUserName();

        User dbUser = userDao.selectByEmail(userName);
        if (dbUser==null){
            return false;
        }else{
            return true;
        }

    }

    @Override
    public User checkLoginByUserName(User user) {
        String userName = user.getUserName();
        String password = user.getPassword();
        User dbUser = userDao.selectByUserName(userName);
        if (userName==null||userName.trim().equals("")){
            throw new RuntimeException("用户为空！");
        }
        if (dbUser==null){
            throw new RuntimeException("没有此用户！");
        }
        /*if (!dbUser.getUserName().equals(userName)){
            throw new RuntimeException("没有此用户！");
        }*/
        if (password==null||password.trim().equals("")){
            throw new RuntimeException("密码为空！");
        }
        if (!dbUser.getPassword().equals(password)){
            throw new RuntimeException("密码错误！");
        }
        return dbUser;
    }

    @Override
    public User checkRegisterByEmail(User user) {
        String email = user.getEmail();
        String password = user.getPassword();
        User dbUser = userDao.selectByEmail(email);
        if (email==null||email.trim().equals("")){
            throw new RuntimeException("用户为空！");
        }
        if (dbUser==null){
            throw new RuntimeException("没有此用户！");
        }
        /*if (!dbUser.getUserName().equals(userName)){
            throw new RuntimeException("没有此用户！");
        }*/
        if (password==null||password.trim().equals("")){
            throw new RuntimeException("密码为空！");
        }
        if (!dbUser.getPassword().equals(password)){
            throw new RuntimeException("密码错误！");
        }
        return dbUser;
    }

    @Override
    public User checkRegisterParams(User user,String code,String checkBox) {
        String email = user.getEmail();
        String password = user.getPassword();
        User dbUser = userDao.selectByEmail(email);

        if (email==null||email.trim().equals("")){
            throw new RuntimeException("邮箱为空！");
        }
        if (dbUser!=null){
            throw new RuntimeException("邮箱已注册！");
        }
        if (password==null||password.trim().equals("")){
            throw new RuntimeException("密码为空！");
        }
        if (code==null||code.trim().equals("")){
            throw new RuntimeException("验证码为空！");
        }
        if (!code.equals(SendEmailController.codeMap.get(email))){
            throw new RuntimeException("验证码不正确！");
        }
        if (checkBox.equals("false")){
            throw new RuntimeException("请勾选《服务协议》！");
        }
        return dbUser;
    }

    @Override
    public User updateUser(long id , User user) {
        int result=userDao.updateByID(id,user);
        if (result !=1){
            throw new RuntimeException("用户信息保存失败");
        }
        User dbUser = userDao.selectByID(id);
        return dbUser;
    }

    @Override
    public User NewOldUser(User user , long id) {
        User oldUser = userDao.selectByID(id);
        if (user.getNickName()==null||user.getNickName().equals("")){
            user.setNickName(oldUser.getNickName());
        }
        if (user.getRealName()==null||user.getRealName().equals("")){
            user.setRealName(oldUser.getRealName());
        }
        if (user.getSex()==null||user.getSex().equals("")){
            user.setSex(oldUser.getSex());
        }
        if (user.getBirthday()==null||user.getBirthday().equals("")){
            user.setBirthday(oldUser.getBirthday());
        }
        if (user.getPhone()==null||user.getPhone().equals("")){
            user.setPhone(oldUser.getPhone());
        }
        if (user.getEmail()==null||user.getEmail().equals("")){
            user.setEmail(oldUser.getEmail());
        }
        int insert = userDao.updateByID(id , user);
        if (insert!=1){
            throw new RuntimeException("修改失败");
        }
        User dbUser = userDao.selectByID(id);
        return dbUser;
    }

    @Override
    public User changePassword(HttpServletRequest req,User user) {
        //得到前端的参数
        String oldPassword = req.getParameter("oldPassword");
        String newPassword1 = req.getParameter("newPassword1");
        String newPassword2 = req.getParameter("newPassword2");

        //校验参数
        if (oldPassword==null||oldPassword.trim().equals("")){
            throw new RuntimeException("原密码为空！");
        }
        if (!user.getPassword().equals(oldPassword)){
            throw new RuntimeException("原密码错误！");
        }
        if (newPassword1==null||newPassword1.trim().equals("")){
            throw new RuntimeException("新密码为空！");
        }
        if (newPassword2==null||newPassword2.trim().equals("")){
            throw new RuntimeException("确认密码为空！");
        }
        if (!newPassword1.equals(newPassword2)){
            throw new RuntimeException("输入的两次密码不相同！");
        }

        //修改数据库的密码
        int result = userDao.changePasswordByID(user.getId() , newPassword2);
        if (result!=1){
            throw new RuntimeException("修改密码失败！");
        }
        User dbUser = userDao.selectByID(user.getId());
        return dbUser;
    }

    @Override
    public User verifyEmail(User user , String email , String code) {
        User userByID = userDao.selectByID(user.getId());
        //是否为空 是否已存在 验证码是否正确
        if (email==null||email.trim().equals("")){
            throw new RuntimeException("输入的邮箱地址为空！请重新输入");
        }
        if (userDao.selectByEmail(email).getId()!=user.getId()){
            throw new RuntimeException("您输入的邮箱地址已存在!");
        }
        if (!code.equals(SendEmailController.codeMap.get(email))){
            throw new RuntimeException("验证码不正确！");
        }
        //修改数据库的邮箱
        int result = userDao.updateEmailByID(user.getId() , email);
        if (result!=1){
            throw new RuntimeException("修改邮箱失败，请稍后再试！");
        }
        User dbUser = userDao.selectByID(user.getId());
        return dbUser;
    }
}
